module(..., package.seeall)
local json = require "json"
--[[Load Text in file *.lang]]
function loadText()		
	local path = system.pathForFile("hardText.lang", system.ResourceDirectory)
	local file = io.open(path, "r")	
	if file then -- nil if no file found
		local texts = {}
		local index = 1
		while true do
			local line = file:read()			
			if line == nil then break end			
			texts[index] = line
			index = index + 1
		end
		io.close( file )
		return texts
	else		
		return nil
	end
end --]]

--[[Convert String To Bytes Array]]
function convertStringToBytes(str)
    local bytes = {}
    local strLength = #str
    for i=1,strLength do
        table.insert(bytes, string.byte(str, i))
    end 
    return bytes
end --]]
 
--[[Convert Bytes Array To String]]
function convertBytesToString(bytes)
    local bytesLength = table.getn(bytes)
    local str = ""
    for i=1,bytesLength do
        str = str .. string.char(bytes[i])
    end
    return str
end --]]

--[[Xor a String]]
function xorString(str,key)
	local stringByte = string.byte
	local stringChar = string.char
	local stringSub = string.sub	
    local strLen = #str
	local keyLen = #key
	local strOut = ""
    for i=1,strLen do
        local c = stringByte(str, i)
		if i<=keyLen then
			local k = stringByte(key, i)
			c = wBitWise.bit_xor(c,k)
			strOut = strOut .. stringChar(c)
		end
    end 
	if #strOut < #str then
		strOut = strOut .. stringSub(str,#strOut+1)
	end
    return strOut
end

--[[Save table in file ]]
function save( filename, dataTable )
	filename = filename..".json"
	print (filename)
	local jsonString = json.encode( dataTable )
	local path = system.pathForFile( filename, _G.path.assetUpdate)
	local file = io.open( path, "w" )
	if file then
	   file:write( jsonString )
	   io.close( file )
	end
end

--[[Load file to table]]
function load( filename )
	filename = filename..".json"
	local base = _G.path.assetUpdate
	local path = system.pathForFile( filename, base )
	local contents
	local file = io.open( path, "r" )
	if file then
		contents = file:read( "*a" )
		io.close( file )
		return json.decode(contents)
	else
		return nil
	end
end
